package timerservice;


import timerservice.listener.RunListener;
import timerservice.util.IDCodeUtil;

import java.util.Date;
import java.util.TimerTask;

public class MyTimerTask extends TimerTask {
    Date firstDate = null;//��һ��ִ��ʱ��
    RunListener.RunOnCompleteListener onCompleteListener;
    RunListener.RunOnCompleteListener onQueCompleteListener;//���ж����������
    RunListener.RunOnProgressListener onProgressListener;
    RunListener.RunOnStartListener onStartListener;
    int targetCount = -1;//Ŀ�����
    String taskCode = "";
    TimerTaskManager manager = null;
    private long period = -1;//ѭ������ʱ��,Ϊ-1ʱ��ѭ������
    private long delay = 0;//�ӳ�ִ��ʱ�䣨����firstDate��delay��Ч��
    private int runCount = 0;//ʵ�����д���
    private Runnable runnable;

    public MyTimerTask(Runnable runnable) {
        this.runnable = runnable;
        this.taskCode = IDCodeUtil.generateUniqueID();
    }

    public MyTimerTask(Runnable runnable, long delay, long period, int target) {
        this.runnable = runnable;
        this.delay = delay;
        this.period = period;
        this.targetCount = target;
        this.taskCode = IDCodeUtil.generateUniqueID();
    }

    public MyTimerTask(Runnable runnable, long delay, long period) {
        this(runnable,delay,period,1);
    }

    public MyTimerTask() {
        this.taskCode = IDCodeUtil.generateUniqueID();
    }

    public MyTimerTask(String _taskCode) {
        if (_taskCode == null || _taskCode.trim().equals("")) {
            _taskCode = IDCodeUtil.generateUniqueID();
        }
        this.taskCode = _taskCode;
    }

    public RunListener.RunOnCompleteListener getOnQueCompleteListener() {
        return onQueCompleteListener;
    }

    public void setOnQueCompleteListener(RunListener.RunOnCompleteListener onQueCompleteListener) {
        this.onQueCompleteListener = onQueCompleteListener;
    }

    public void run() {
        if (runCount == 0 && onStartListener != null) {
            onStartListener.onStart(taskCode, targetCount);
        }
        if (runnable != null) {
            runnable.run();
        } else {
            execute();
        }
        if (period == -1) {//ִֻ��һ��
            manager.deleteTimerTaskByCode(taskCode, true);
            manager = null;
            if (onCompleteListener != null) onCompleteListener.onComplete(taskCode, targetCount);
            return;
        }
        runCount++;
        if (onProgressListener != null) onProgressListener.onProgress(taskCode, runCount, targetCount);
        if (runCount == targetCount) {
            manager.deleteTimerTaskByCode(taskCode, true);
            if (onCompleteListener != null) onCompleteListener.onComplete(taskCode, targetCount);
            if (onQueCompleteListener != null) onQueCompleteListener.onComplete(taskCode, targetCount);
            if (manager.hmTimerTask.isEmpty()&&manager.taskQue.isEmpty()){
                manager.timer.cancel();
                System.exit(0);
            }
        }
    }

    public void execute() {
    }

    public Date getFirstDate() {
        return firstDate;
    }

    public void setFirstDate(Date firstDate) {
        this.firstDate = firstDate;
    }

    public RunListener.RunOnCompleteListener getOnCompleteListener() {
        return onCompleteListener;
    }

    public void setOnCompleteListener(RunListener.RunOnCompleteListener onCompleteListener) {
        this.onCompleteListener = onCompleteListener;
    }

    public RunListener.RunOnProgressListener getOnProgressListener() {
        return onProgressListener;
    }

    public void setOnProgressListener(RunListener.RunOnProgressListener onProgressListener) {
        this.onProgressListener = onProgressListener;
    }

    public RunListener.RunOnStartListener getOnStartListener() {
        return onStartListener;
    }

    public void setOnStartListener(RunListener.RunOnStartListener onStartListener) {
        this.onStartListener = onStartListener;
    }

    public int getTargetCount() {
        return targetCount;
    }

    public void setTargetCount(int targetCount) {
        this.targetCount = targetCount;
    }

    public String getTaskCode() {
        return taskCode;
    }

    public void setTaskCode(String taskCode) {
        this.taskCode = taskCode;
    }

    public TimerTaskManager getManager() {
        return manager;
    }

    public void setManager(TimerTaskManager manager) {
        this.manager = manager;
    }

    public long getPeriod() {
        return period;
    }

    public void setPeriod(long period) {
        this.period = period;
    }

    public long getDelay() {
        return delay;
    }

    public void setDelay(long delay) {
        this.delay = delay;
    }

    public int getRunCount() {
        return runCount;
    }

    public void setRunCount(int runCount) {
        this.runCount = runCount;
    }

    public void setTimerTaskManager(TimerTaskManager manager) {
        this.manager = manager;
    }

}